var oldOpen = null, windowH = 0, isOpen = true; // 上一次点击的详情
$(function () {
    $(".activity-left-opts span").click(function(e){
        e.stopPropagation();
        $(this).siblings("span").removeClass("selected");
        $(".activity-project").css("display", "none");
        $(this).addClass("selected");
        var index = $(this).index();
        $(".activity-content-"+index).css("display","block"); // 显示选项卡对应的内容
        closeDialog();
        oldOpen = null; //
        isOpen = true;
        $(".activity-detail-wrap").addClass("close-status"); // 关闭弹窗
        if($(".activity-content-"+index).find(".activity-pic").length === 0){

        }
        if(index == 0){
            income.getBalanceList(1);
        }else if(index == 1){
            income.getSettleList(1);
        }
    });

    document.getElementById("right-bottom-content").onclick = function(e){ // 展开详情
        e.stopPropagation();
        if(e.target.className && e.target.className.indexOf("open-detail") > -1){
            changetitle(e.target);
            return;
        }else{
            closeDialog();
        }
        if(e.target.parentNode.className && e.target.parentNode.className.indexOf("open-detail") > -1){
            changetitle(e.target.parentNode);
            return;
        }else{
            closeDialog();
        }
    }
    document.body.onclick = function(e){
        if(e.target.className && e.target.className.indexOf("open-detail") === -1){
            closeDialog();
            return;
        }
        if(e.target.parentNode.className && e.target.parentNode.className.indexOf("open-detail") === -1){
            closeDialog();
            return;
        }
    }
});

window.onresize = function(e){
    e.preventDefault();
    // setWindow();
}
window.onload = function(e){
    e.preventDefault();
    // setWindow();
}
function setWindow(){ // 计算位置
    var height = document.documentElement.clientHeight;
    windowH = height-74-39-296-2;
    document.getElementById("right-bottom-content").style.height = height - 74 - 39 + "px";
    document.getElementById("activity-table-0").style.cssText = "height:"+windowH+"px;overflow-y:auto;margin-bottom:0;padding-bottom:0;";
    document.getElementById("activity-table-1").style.cssText = "height:"+windowH+"px;overflow-y:auto;margin-bottom:0;padding-bottom:0;";
}

function setDialogW(obj){ // 计算弹窗位置
    var index = $(".activity-left-opts").find(".selected").index();
    var eleW = $("#activity-table-"+index+" .open-detail").outerWidth();
    var dialogW = $("#activity-table-"+index).innerWidth(); // 详情弹窗宽度
    var result = document.getElementById("activity-table-"+index).scrollHeight > windowH?14:0;
    obj.children[2].style.width = dialogW-4-result+"px";
    obj.children[2].children[0].style.right = eleW-26+"px";
}

function changetitle(obj){
    var wrap = $(obj).parents(".sp-btm-content")[0];
    var left = getElemPos(obj).x - document.getElementById("left-content").offsetWidth-70;
    var title = obj.children[0], arrow = obj.children[1], dialog = obj.children[2];
    var boxIndex = $(".activity-left-opts").find(".selected").index(); // 获取选中的选项卡
    if(oldOpen && oldOpen !== obj){
        oldOpen.children[0].innerHTML = "详情";
        oldOpen.children[1].className = "fa fa-angle-down";
        oldOpen.children[2].className = "activity-detail-wrap close-status";
    }
    if(isOpen){
        title.innerHTML = "收起";
        arrow.className = "fa fa-angle-up";
        dialog.className = "activity-detail-wrap";
    }else{
        title.innerHTML = "详情";
        arrow.className = "fa fa-angle-down";
        dialog.className = "activity-detail-wrap close-status";
    }
    isOpen = !isOpen; // 点击打开后，判断取反
    oldOpen = obj;
    dialog.style.top = 37+"px";
    dialog.style.right = 0;
    setDialogW(obj);
}
function closeDialog(){
    if(oldOpen){
        oldOpen.children[0].innerHTML = "详情";
        oldOpen.children[1].className = "fa fa-angle-down";
        oldOpen.children[2].className = "activity-detail-wrap close-status";
    }
}

function getElemPos(obj){ // 获取元素的绝对位置
    var pos = {"top":0, "left":0};
    if (obj.offsetParent){
        while (obj.offsetParent){
            pos.top += obj.offsetTop;
            pos.left += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }else if(obj.x){
        pos.left += obj.x;
    }else if(obj.x){
        pos.top += obj.y;
    }
    return {x:pos.left, y:pos.top};
}

var income = {
    getBalanceList: function (pageId) {
        incomeRequest.getBalanceList(pageId, function (result) {
            var _el = $('#balance_list');
            _el.html('');
            var data = result.data;
            if (data != null && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var balance_item = template("balance_item", data[i]);
                    _el.append(balance_item);
                }
            } else {
                if (pageId == 1) {
                    $('#balance_list').append('<div class="no-data-box"><img class="no-data-img" src="/Public/image/www/no-data.png" alt="这是图片" /><p class="no-data-title">这里什么也没有</p></div>');
                }
            }
        });
    },
    getSettleList: function (pageId) {
        incomeRequest.getSettleList(pageId, function (result) {
            var _el = $('#settle_list');
            _el.html('');
            var data = result.data;
            if (data != null && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var balance_item = template("balance_item", data[i]);
                    _el.append(balance_item);
                }
            } else {
                if (pageId == 1) {
                    $('#settle_list').append('<div class="no-data-box"><img class="no-data-img" src="/Public/image/www/no-data.png" alt="这是图片" /><p class="no-data-title">这里什么也没有</p></div>');
                }
            }
        });
    }
};


var incomeRequest = {
    getBalanceList: function (pageId, callback) {
        $.ajax({
            type: "post",
            url: "/Funds/getBalanceList",
            data: {pageId: pageId},
            dataType: "json",
            success: function (result) {
                if (checkAjaxResult(result)) {
                    callback(result);
                } else {
                    layer.msg(result.msg);
                }
            }
        })
    },
    getSettleList: function (pageId, callback) {
        $.ajax({
            type: "post",
            url: "/Funds/getSettleList",
            data: {pageId: pageId},
            dataType: "json",
            success: function (result) {
                if (checkAjaxResult(result)) {
                    callback(result);
                } else {
                    layer.msg(result.msg);
                }
            }
        })
    },
};